package com.itheima.controller;

import com.itheima.aop.annotation.LogOperater;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：cxl
 * @Package：com.itheima.controller
 * @Project：web-project
 * @name：StudentController
 * @Date：2025-09-29 8:08 pm
 * @Filename：StudentController
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    StudentService studentService;

    /*学员列表 分页条件查询*/

    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        log.info("StudentController传回前端的pageResult{}", pageResult);
        return Result.success(pageResult);
    }

    /*批量删除学员*/
    @LogOperater
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
        log.info("StudentController接收前端发过来需要删除的ids={}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /*添加学员*/
    @LogOperater
    @PostMapping
    public Result add(@RequestBody Student student) {
        studentService.add(student);
        return Result.success();
    }

    /*根据id查询学员*/
    @LogOperater
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Student student = studentService.selectById(id);
        return Result.success(student);
    }

    /*修改学员*/
    @LogOperater
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("学生修改的Controller获得的参数student={}", student);
        studentService.update(student);
        return Result.success();
    }

    /*违纪处理*/
    @LogOperater
    @PutMapping("violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("violationController传过来的id={},score={}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }

}
